package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.CompanyContentLabelRequest;
import com.jmp.api.admin.dto.CompanyContentListRequest;
import com.jmp.api.admin.dto.CompanyContentListResponse;
import com.jmp.api.admin.entity.CompanyContentEntity;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.ArticleApi;
import com.jmp.base.LabelApi;
import com.jmp.base.dto.LabelRelateRequest;
import com.jmp.feign.LabelFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "企业管理-推送权限")
@RestController
@RequestMapping("adminApi/company/content")
@Slf4j
public class CompanyContentController {
    @Autowired
    private LabelFeign labelFeign;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;

    @ApiOperation(value = "列表")
    @PostMapping("list")
    @SecurityAuthCheck
    public ResultModel<CompanyContentListResponse> auditCompany(CompanyContentListRequest request) {

        PageHelper.startPage(1, 100);// 100 标签应该不会超过100
        List<CompanyCustomerLable> labels = companyApiAdminMapper.selectLabel(ArticleApi.DEFAULT_COMPANYID, null);
        List<CompanyContentListResponse.Label> labelList = ConvertUtils.list(labels, label -> CompanyContentListResponse.Label.builder()
                .id(label.getLableId())
                .name(label.getLableName())
                .build());

        String nameLike = SqlUtil.like(request.getNameLike());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyContentEntity> companies = companyApiAdminMapper.selectCompanyContent(nameLike);
        PageResponse2<CompanyContentListResponse.Company> companyList = ConvertUtils.page2(companies, companyContentEntity -> {
            List<Integer> authLabelIds = companyApiAdminMapper.selectLabelByCompany(LabelApi.LABEL_TYPE_COMPANY_AUTH, companyContentEntity.getCompanyId());
            List<String> authLabelNames = companyApiAdminMapper.selectLabelName(LabelApi.LABEL_TYPE_COMPANY_AUTH, companyContentEntity.getCompanyId());
            return CompanyContentListResponse.Company.builder()
                    .id(companyContentEntity.getId())
                    .companyId(companyContentEntity.getCompanyId())
                    .companyName(companyContentEntity.getCompanyName())
                    .pushNum(companyContentEntity.getPushNum())
                    .labelIds(authLabelIds)
                    .labelNames(authLabelNames)
                    .build();
        });

        CompanyContentListResponse response = CompanyContentListResponse.builder()
                .labelList(labelList)
                .companyList(companyList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "更新标签")
    @PostMapping("label")
    @SecurityAuthCheck
    public ResultModel label(@RequestBody CompanyContentLabelRequest request) {
        //企业权限标签
        LabelRelateRequest deleteRequest2 = LabelRelateRequest.builder()
                .type(LabelApi.LABEL_TYPE_COMPANY_AUTH)
                .typeId(request.getCompanyId())
                .labelIds(request.getLabelIds())
                .build();
        ResultModel resultModel2 = labelFeign.relate(deleteRequest2);
        AssertUtil.isTrue(resultModel2);
        return ResultModel.success();
    }
}
